package com.mall.order.biz.handler;

import com.mall.commons.tool.utils.TradeNoUtils;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CartProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/8/1-下午5:01
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext contexts=(CreateOrderContext)context;
        String orderId = TradeNoUtils.generateTradeNo();

        //添加一个订单order
        Order order = new Order();
        order.setOrderId(orderId);
        order.setPayment(contexts.getOrderTotal());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setUserId(contexts.getUserId());
        order.setBuyerNick(contexts.getBuyerNickName());
        order.setUniqueKey(contexts.getUniqueKey());
        orderMapper.insert(order);

        //添加一个order_item
        List<CartProductDto> cartProductDtoList = contexts.getCartProductDtoList();
        List<Long> idList = new ArrayList<>();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            String orderitemId = TradeNoUtils.generateTradeNo();
            orderItem.setId(orderitemId);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setOrderId(orderId);
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());

            Double sum = cartProductDto.getSalePrice().doubleValue()*cartProductDto.getProductNum().intValue();
            orderItem.setTotalFee(sum);//总价格可能有问题

            String[] split = cartProductDto.getProductImg().split(",");
            orderItem.setPicPath(split[0]);
            orderItem.setStatus(1);
            orderItem.setCreateTime(new Date());
            orderItem.setUpdateTime(new Date());
            orderItemMapper.insert(orderItem);
            idList.add(cartProductDto.getProductId());
        }
        contexts.setOrderId(orderId);
        contexts.setBuyProductIds(idList);

        System.out.println(contexts);
        System.out.println("11111111111");
        return true;
    }
}
